package components.plugins.effects
{
	import UIUtils.uif_util;
	
	import flash.display.BitmapData;
	import flash.filters.BitmapFilter;
	import flash.filters.BlurFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class clte_blur extends clt_baseEffect {
		protected var _filter: BitmapFilter;
		
		public function clte_blur(a_params:Object) {
			super(uif_util.unionObj({
					blur		: 0,
					
					steps		: 1,
					alphaStep	: 0.3,
					scale		: 0,
					center		: [0.5, 0.1],
					roll		: 0
				}, a_params)
			);
			
			updateFilter();
		}
		
		override public function execute(bitmapData: BitmapData):void {
			var a_tmp: BitmapData = copyBD(bitmapData);
			var source: BitmapData = copyBD(bitmapData);
			
			for (var step: int = 0; step<params.steps; step++) {
				var ct	: ColorTransform = new ColorTransform(1, 1, 1, params.alphaStep);
				a_tmp.applyFilter(source, bitmapData.rect, new Point(), _filter);
				bitmapData.draw(a_tmp, matrixStep(step, bitmapData.rect), ct, blendMode);
			}
			source.dispose();
			a_tmp.dispose();
		}
		
		protected function matrixStep(a_step: int, bmRect: Rectangle): Matrix {
			var mat: Matrix = new Matrix();
			mat.translate(-bmRect.width * params.center[0], -bmRect.height  * params.center[1]);
			mat.scale(1 + params.scale * a_step, 1 + params.scale * a_step);
			mat.rotate(a_step * params.roll);
			mat.translate(bmRect.width * params.center[0], bmRect.height  * params.center[1]);
			return mat;
		}
		
		protected function updateFilter(): void {
			_filter = new BlurFilter(params.blur, params.blur, 3);
		}
		
		override public function incValue3():void {
			params.steps = changeValue(params.steps, 1, 10);
		}
		
		override public function incValue2():void {
			params.scale = changeValue(params.scale, 0.002, 1);
		}
		
		override protected function getParamName():Array {
			return [
				locale.BLUR,
				locale.SCALE,
				locale.STEPS,
				locale.BLENDMODE,
				locale.ALPHA
			];
		}
		
		override protected function getValues():Array {
			return [
				params.blur * 10,
				params.scale * 100,
				params.steps * 10,
				blendMode100,
				params.alphaStep * 100
			];
		}
		
		override protected function setValues(a_set:Array):void {
			params.blur  = a_set[0] * .1;
			params.scale = a_set[1] * .01;
			params.steps = a_set[2] * .1;
			params.alphaStep = a_set[4] * .01;
			updateFilter();
			super.setValues(a_set);
		}
	}
}